<?php
/**
 * Created by PhpStorm.
 * User: Ren
 * Date: 4/11/16
 * Time: 2:57 PM
 */

namespace app\controllers;
use yii\web\Controller;
use app\models\Rs_raised;
use app\models\Uc_user;
use app\models\Pr_support;
use Yii;
use yii\helpers\Url;
use app\models\UploadFormw;
use yii\web\UploadedFile;
use app\models\Uc_qualification;

define('HOST', 'http://localhost/chuangwu/web/index.php?r=');

class CooperatorapplyController extends Controller
{
    public function actionShow(){

        if(!empty($_COOKIE['user_id'])){
            $user_id = $_COOKIE['user_id'];
            $user = Uc_user::find()->where([
                'user_id' => $user_id,
                'del_flag' => 0
            ])->one();

            if($user['is_cooperator'] == 1){
                $this->redirect(['qulification/show']);
            }
            if($user['is_investor'] == 0){

                echo "<script>alert(\"请先成为投资者\");</script>";
                echo "<script>location.href = \"http://localhost/chuangwu/web/index.php?r=qulification/show\";</script>";

            }else{

                $view = Yii::$app->view;
                $view->params['title'] = "申请成为推荐人";
                PrlikeController::setView($user);


                $model = new UploadFormw();
                if (Yii::$app->request->isPost) {
                    $model->file1 = UploadedFile::getInstance($model, 'file1');
                    $model->file2 = UploadedFile::getInstance($model, 'file2');
                    $model->file3 = UploadedFile::getInstance($model, 'file3');
                    $model->file4 = UploadedFile::getInstance($model, 'file4');
                    $model->file5 = UploadedFile::getInstance($model, 'file5');

                    $collection = "";
                    if ($model->validate()) {
                        if ($model->file1) {

                            $model->file1->saveAs('uploads/' . $model->file1->baseName . '.' . $model->file1->extension);
                            $collection = $collection .  'uploads/' . $model->file1->baseName . '.' . $model->file1->extension . ';';
                        }

                        if ($model->file2) {
                            $model->file2->saveAs('uploads/' . $model->file2->baseName . '.' . $model->file2->extension);
                            $collection = $collection . 'uploads/' . $model->file2->baseName . '.' . $model->file2->extension . ';';

                        }
                        if ($model->file3) {
                            $model->file3->saveAs('uploads/' . $model->file3->baseName . '.' . $model->file3->extension);
                            $collection = $collection . 'uploads/' . $model->file3->baseName . '.' . $model->file3->extension . ';';

                        }
                        if ($model->file4) {
                            $model->file4->saveAs('uploads/' . $model->file4->baseName . '.' . $model->file4->extension);
                            $collection = $collection . 'uploads/' . $model->file4->baseName . '.' . $model->file4->extension . ';';
                        }
                        if ($model->file5) {
                            $model->file5->saveAs('uploads/' . $model->file5->baseName . '.' . $model->file5->extension);
                            $collection = $collection . 'uploads/' . $model->file5->baseName . '.' . $model->file5->extension . ';';

                        }

                        $query = Uc_qualification::findOne([
                            'user_id' => $user_id,
                            'del_flag' => 0,
                        ]);
                        if ($query == null) {
                            echo "<script>alert('您还不是投资者')</script>";
                            echo "<script>location.href = \"http://localhost/chuangwu/web/index.php?r=qulification/show\";</script>";
                        } else {
                            $query['coqulityImage'] = $collection;
                            $query['type'] = 1;
                            $query->save();
                        }


                        echo "<script>alert('您的申请已经提交...请耐心等待')</script>";
                        echo "<script>location.href = \"http://localhost/chuangwu/web/index.php?r=qulification/show\";</script>";
                    }
                }

                return $this->render('cooperatorapply',  ['user'=>$user, 'model'=>$model]);

            }
        }else{
            echo "<script>alert('cookie 不存在')</script>";
            // sleep(3);
            $this->redirect(['login/show']);
        }


    }
}